<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>日志列表</title>
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" media="all">
    <link rel="stylesheet" th:href="@{/font-awesome-4.7.0/css/font-awesome.min.css}">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline" style="width: 250px">
                    <label class="layui-form-label">管理员</label>
                    <div class="layui-input-block">
                        <input type="text" id="username" name="username" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline" style="width: 290px">
                    <label class="layui-form-label">时间范围</label>
                    <div class="layui-input-block">
                        <input type="text" name="logTime" lay-verify="required" placeholder="时间" autocomplete="off" class="layui-input" id="logTime">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" data-type="search_table">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="batchdel">删除</button>
            </div>

            <table id="table" lay-filter="table"></table>


        </div>
    </div>
</div>

<script th:src="@{/layuiadmin/layui/layui.js}"></script>

<script>

    layui.use(['jquery','layer', 'table','laydate'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        var laydate = layui.laydate;

        laydate.render({
            elem: '#logTime',
            range: true
        });

        //执行一个 table 实例
        table.render({
            elem: '#table',
            height: 450,
            url: '/badminton/logList', //数据接口
            title: '日志列表',
            page: true,
            limit: 20,
            limits: [5,10,15,20],
            cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'id', title: '编号', width:70},
                    {field: 'username', title: '管理员', width:150},
                    {field: 'logTime', title: '时间', width: 300},
                    {field: 'uri', title: '访问路径', width:200}
                ]
            ],
            //用于搜索结果重载
            id: 'sreload'
        });

        var active = {
            search_table: function(){
                var username = $('#username');
                var logTime = $('#logTime');

                //执行重载
                table.reload('sreload', {
                    //一定要加不然乱码
                    method: 'post'
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        //表示传到后台的参数
                        username: username.val(),
                        logTime:logTime.val()
                    }
                });
            },
            batchdel:function () {
                var checkAll = table.checkStatus('sreload');
                if(checkAll.data.length == 0){
                    layer.msg('请选择数据进行删除');
                }else{
                    layer.confirm('真的删除行么', function(){
                        var jsonObj = {};
                        for(var i=0;i<checkAll.data.length;i++){
                            jsonObj["ids["+i+"]"] = checkAll.data[i].id;
                        }
                        $.ajax({
                            url:'/badminton/delLog',
                            data:jsonObj,
                            dataType:'json',
                            type:'post',
                            success:function (data) {
                                if (data.state){
                                    window.location.reload();
                                }else{
                                    layer.msg(data.message);
                                }
                            }
                        })
                    });
                }

            }
        };

        $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });


</script>
</body>
</html>
